package logic

import (
	"2210a/commod/models/mysql"
	"context"
	"fmt"
	"gorm.io/gorm"

	"2210a/commod/commodrpc/commodrpc"
	"2210a/commod/commodrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ShopUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShopUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShopUpdateLogic {
	return &ShopUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ShopUpdateLogic) ShopUpdate(in *commodrpc.ShopUpdateRequest) (*commodrpc.ShopUpdateResponse, error) {
	// todo: add your logic here and delete this line
	var FindProduct mysql.Products
	mysql.Db.Where("id = ?", in.Id).Find(&FindProduct)
	if FindProduct.ID == 0 {
		return nil, fmt.Errorf("商品不存在，修改失败")
	}
	if FindProduct.UserId != in.UserId {
		return nil, fmt.Errorf("用户不存在，修改失败")
	}

	products := mysql.Products{
		Model:     gorm.Model{ID: uint(in.Id)},
		UserId:    in.UserId,
		Avatar:    in.Avatar,
		ShopName:  in.ShopName,
		Price:     in.Price,
		Address:   in.Address,
		Quantity:  in.Quantity,
		Introduce: in.Introduce,
	}
	mysql.Db.Debug().Updates(&products)
	return &commodrpc.ShopUpdateResponse{Id: int64(products.ID)}, nil
}
